package com.csw.mapreduce;

//1500101000,1000008,87

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo03SumScore {
    //map过程
    //创建静态类  继承Mapper类  给上输入输出形参
    public static class ScoreSumMap extends Mapper<LongWritable,Text,Text,IntWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            System.out.println("Map");
            //Context承上启下  map中进行连接
            //一行一行获取数据 以","拆分，[0]为id[2]为score
            String line = value.toString();
            String[] split = line.split(",");
            String id = split[0];
            int score = Integer.parseInt(split[2]);
            context.write(new Text(id),new IntWritable(score));
        }
    }
    public static class ScoreSumReduce extends Reducer<Text,IntWritable,Text,IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            //统计学生总分
            int sum=0;
            //对Iterable<LongWritable> values进行增强for获取一个一个kv键值对
            //reduce之前shuffle过程中相同的k存储到同一个reduce中
            for (IntWritable value : values) {
                int i = value.get();
                sum+=i;
            }
            //输出结果获取当前单词
            String id = key.toString();
            //把结果输出到hdfs
            context.write(new Text(id),new IntWritable(sum));
        }
    }
    //启动mapreduce,并且连接这两个独立的任务
    public static void main(String[] args) throws Exception {
        //构建一个job
        Job job = Job.getInstance();
        //指定job任务名称(不指定也可以 为系统默认的名称)
        job.setJobName("scoresum");
        //指定reduce的task为2
        job.setNumReduceTasks(2);
        //指定当前main所在类名
        job.setJarByClass(Demo03SumScore.class);

        //指定map类
        job.setMapperClass(ScoreSumMap.class);
        //指定map输出的k和v的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //指定reduce
        job.setReducerClass(ScoreSumReduce.class);
        //指定reduce输出k和v的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //指定输入路径
        FileInputFormat.addInputPath(job,new Path("/data/score.txt"));
        //获取fileSystem对象，hdfs文件管理系统的对象
        FileSystem fs = FileSystem.get(new Configuration());
        Path path = new Path("/data/out2");
        //指定输出路径(路径不能已存在),如果已存在删除
        if(fs.exists(path)){
            fs.delete(path,true);
        }
        //指定输出路径(路径不能已存在)
        FileOutputFormat.setOutputPath(job,path);
        //启动
        job.waitForCompletion(true);
        System.out.println("job任务");
    }
}
